<template>
    <h2>添加学生</h2>
    <div>
        <!-- -->
        <p>
            学号:<input v-model="student.studentNo" />
        </p>
        <p>
            密码:<input v-model="student.loginPwd" />
        </p>
        <p>
            姓名:<input v-model="student.studentName" />
        </p>
        <p>
            性别:
            <input type="radio" name="sex" value="男" v-model="student.sex" /> 男
            <input type="radio" name="sex" value="女" v-model="student.sex" /> 女
        </p>
        <p>
            所在年级:
            <select v-model="student.gradeId">
                <option value="0">请选择</option>
                <option v-for="item in grades" :value="item.gradeID">
                    {{ item.gradeName }}
                </option>
            </select>
        </p>
        <p>
            电话号码:<input v-model="student.phone" />
        </p>
        <p>
            地址:<input v-model="student.address" />
        </p>
        <p>
            出生日期:<input v-model="student.bornDate" />
        </p>
        <p>
            <button @click="handleClick">提交</button>
        </p>
    </div>
</template>
<script>
import axios from 'axios';
export default {
    data() {
        return {
            grades: [],
            student: {
                studentNo: "",
                loginPwd: "",
                studentName: "",
                sex: "男",
                gradeId: 0,
                phone: "",
                address: "",
                bornDate: ""
            }
        }
    },
    methods: {
        loadgrades() {
            axios.get('/grade/all',).then(res => {
                if (res.data.success) {
                    this.grades = res.data.data
                }
            })
        },
        handleClick() {
            axios.post('/student', this.student)
                .then(res => {
                    if (res.data.success) {
                        alert('添加成功')
                        this.student = {
                            studentNo: "",
                            loginPwd: "",
                            studentName: "",
                            sex: "男",
                            gradeId: 0,
                            phone: "",
                            address: "",
                            bornDate: ""
                        }
                    }
                })
        }
    },
    created() {
        this.loadgrades()
    }
}
</script>
<style scoped></style>